<?php

class Defined_RouterControl{
	
	private $oFront;
	
	function __construct(){
		$this->oFront=Zend_Controller_Front::getInstance();
	}
	
	static function defineRoute(){
		$oView=new Zend_View();		
        
		self::addRouter('by_index', '', array(), 'index', 'index');
        
        $skey=$oView->translate('by_type_0');
		self::addRouter('by_type_0', $skey, array(), 'index', 'index');
		
        $skey=$oView->translate('by_type_detail_0');
		self::addRouter('by_type_detail_0', $skey, array(1=>'id',2=>'alias'), 'detail', 'index');
        
        $skey=$oView->translate('by_type_1');
		self::addRouter('by_type_1', $skey, array(), 'index', 'index');
		
        $skey=$oView->translate('by_type_detail_1');
		self::addRouter('by_type_detail_1', $skey, array(1=>'id',2=>'alias'), 'detail', 'index');
        
	    # list content by city router
		$skey=$oView->translate('by_city_0');
		self::addRouter('by_city_0', $skey, array(1=>'city'), 'search', 'list');
		
        #Detail by city
		$skey=$oView->translate('by_city_detail_0');
		self::addRouter('by_city_detail_0', $skey, array(1=>'city', 2=>'id', 3=>'alias'), 'detail', 'index');
        
		# list content by city router
		$skey=$oView->translate('by_city_1');
		self::addRouter('by_city_1', $skey, array(1=>'city'), 'search', 'list');		
		
        #Detail by city
		$skey=$oView->translate('by_city_detail_1');
		self::addRouter('by_city_detail_1', $skey, array(1=>'city', 2=>'id', 3=>'alias'), 'detail', 'index');
        
	    # list content by brand router       
		$skey=$oView->translate('by_brand_0');
		self::addRouter('by_brand_0', $skey, array(1=>'brand'), 'search', 'list');
		
        $skey=$oView->translate('by_brand_detail_0');
		self::addRouter('by_brand_detail_0', $skey, array(1=>'brand', 2=>'id', 3=>'alias'), 'detail', 'index');
        
		# list content by brand router
		$skey=$oView->translate('by_brand_1');
		self::addRouter('by_brand_1', $skey, array(1=>'brand'), 'search', 'list');
		
        $skey=$oView->translate('by_brand_detail_1');
		self::addRouter('by_brand_detail_1', $skey, array(1=>'brand', 2=>'id', 3=>'alias'), 'detail', 'index');
	    
        # list content by price router       
		$skey=$oView->translate('by_price_0');
		self::addRouter('by_price_0', $skey, array(1=>'from', 2=>'to'), 'search', 'list');
        
        $skey=$oView->translate('by_price_detail_0');
		self::addRouter('by_price_detail_0', $skey, array(1=>'from', 2=>'to', 3=>'id', 4=>'alias'), 'detail', 'index');
        
        # list content by price router       
		$skey=$oView->translate('by_price_1');
		self::addRouter('by_price_1', $skey, array(1=>'from', 2=>'to'), 'search', 'list');
		
		$skey=$oView->translate('by_price_detail_1');
		self::addRouter('by_price_detail_1', $skey, array(1=>'from', 2=>'to', 3=>'id', 4=>'alias'), 'detail', 'index');
        
		unset($skey);
		unset($oView);
	}
	
	private function addRouter($sRouteName, $sRegex, $arrRef, $sControllName, $sAction){
		$router=Zend_Controller_Front::getInstance()->getRouter();
		
		$router->addRoute($sRouteName,
			new Zend_Controller_Router_Route_Regex(
								$sRegex,
                                 array('controller' => $sControllName,
                                       'action' => $sAction),
                                 $arrRef,
                                 '%s.html'      
			)
		);
	}
	
	function install(){
		
		$router=$this->oFront->getRouter();

		# add default router
		$routeDefault = new Zend_Controller_Router_Route_Module(
	        array(),
	        $this->oFront->getDispatcher(),
	        $this->oFront->getRequest()
	    );

	    // Add both language route chained with default route and
	    // plain language route
	    $router->addRoute('default', $routeDefault);

	    // Register plugin to handle language changes
	    $this->oFront->registerPlugin(new Defined_Plugin_LanguageHandler());
	}
}
